package org.taam.modules.services;


import java.util.HashMap;

import fr.obeo.acceleo.gen.template.eval.ENodeCastException;

public class JavaToSQLServices {

	private static HashMap<String, String> mapJavaToSQL=null; 
	private static HashMap<String, String> mapSQLToJava=null;
	
	/**
	 * Return the equivalence for java to sql types.
	 */
	public String getSQLType(String s) throws ENodeCastException {
		String result = getJavaToSQLMap().get(s);
		if (result==null) return "INDEFINED TYPE FOR "+s;
		return result;
	}

	private HashMap<String,String> getJavaToSQLMap(){
		if (mapJavaToSQL==null){
			mapJavaToSQL = new HashMap<String, String>();		
			mapJavaToSQL.put("String","VARCHAR(1024)");//	map.put("String","LONGVARCHAR");map.put("String","CHAR");
			mapJavaToSQL.put("java.math.BigDecimal","NUMERIC");
			mapJavaToSQL.put("java.math.BigDecimal","DECIMAL");
			mapJavaToSQL.put("boolean","BIT");
			mapJavaToSQL.put("byte","TINYINT");
			mapJavaToSQL.put("short","SMALLINT");
			mapJavaToSQL.put("int","INTEGER");
			mapJavaToSQL.put("long","BIGINT");
			mapJavaToSQL.put("float","REAL");
			mapJavaToSQL.put("double","FLOAT");
			mapJavaToSQL.put("double","DOUBLE");		
			mapJavaToSQL.put("byte[]","VARBINARY ");//map.put("byte[]","BINARY");map.put("byte[]","LONGVARBINARY");
			mapJavaToSQL.put("Date","DATE");
			mapJavaToSQL.put("Time","TIME");
			mapJavaToSQL.put("TimeStamp","TIMESTAMP");
		}
		return mapJavaToSQL;
	}
	
	/**
	 * Return the equivalence for sql to java types.
	 */
	public String getJavaType(String s) throws ENodeCastException {
		String result = getJavaToSQLMap().get(s);
		if (result==null) return "INDEFINED TYPE FOR "+s;
		return result;
	}

	private HashMap<String,String> getSQLToJavaMap(){
		if (mapSQLToJava==null){
			mapSQLToJava = new HashMap<String, String>();		
			mapSQLToJava.put("String","VARCHAR(1024)");//	map.put("String","LONGVARCHAR");map.put("String","CHAR");
			mapSQLToJava.put("java.math.BigDecimal","NUMERIC");
			mapSQLToJava.put("java.math.BigDecimal","DECIMAL");
			mapSQLToJava.put("boolean","BIT");
			mapSQLToJava.put("byte","TINYINT");
			mapSQLToJava.put("short","SMALLINT");
			mapSQLToJava.put("int","INTEGER");
			mapSQLToJava.put("long","BIGINT");
			mapSQLToJava.put("float","REAL");
			mapSQLToJava.put("double","FLOAT");
			mapSQLToJava.put("double","DOUBLE");		
			mapSQLToJava.put("byte[]","VARBINARY ");//map.put("byte[]","BINARY");map.put("byte[]","LONGVARBINARY");
			mapSQLToJava.put("Date","DATE");
			mapSQLToJava.put("Time","TIME");
			mapSQLToJava.put("TimeStamp","TIMESTAMP");
		}
		return mapSQLToJava;
	}
	
}
